package local.blueheart.data.springdatademo.domino;

import local.blueheart.data.springdatademo.exception.DominoException;
import local.blueheart.data.springdatademo.exception.DominoExceptionEnum;
import lotus.domino.Document;
import lotus.domino.NotesException;
import lotus.domino.View;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DView extends DBase {

    private static final Logger LOGGER = LoggerFactory.getLogger(DView.class);

    public DView(View view){
        super(view);
    }

    /**
     * 从视图返回一个文档
     * @param dDocument
     * @return
     * @throws NotesException
     */
    public DDocument getDocument(DDocument dDocument) throws DominoException {
        try {
            View view = (View) getBase();
            Document document;
            DDocument temp;
            if (dDocument == null) {
                document = view.getFirstDocument();
                temp = new DDocument(document);
            } else {
                LOGGER.debug("dDocument.getBase() = {}",dDocument.getBase());
                Document document1 = (Document) dDocument.getBase();
                document = view.getNextDocument(document1);
                temp = new DDocument(document);
            }
            addChild(temp);
            return temp;
        }catch (NotesException ne){
            LOGGER.error("获取文档出错",ne.fillInStackTrace());
            throw new DominoException(ne, DominoExceptionEnum.GET_DOC_ERR);
        }
    }


}
